package com.witte.controller;

import com.witte.entity.Customer;
import com.witte.entity.Offer;
import com.witte.entity.Orders;
import com.witte.entity.RespBean;
import com.witte.service.CusService;
import com.witte.service.OfferService;
import com.witte.service.OrdersService;
import com.witte.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/api/Sales")
@CrossOrigin
public class OrderController {
    @Autowired
    OrdersService ordersService ;
    @Autowired
    ProductService productService;
    @Autowired
    CusService cusService;

    @GetMapping(value="/order/getOrderList")
    public RespBean getOrderList(Integer beginPage, Integer pageSize){
        // System.out.println("111");
        //Map<String ,List<Object>> res = new HashMap<>();
        //List<Customer> customerList =new ArrayList<>();
        List<Orders> list = ordersService.selectForPage(beginPage, pageSize);
        for(int i =0 ; i<list.size() ;i++){
            list.get(i).setProductid(productService.selectByPrimaryKey(list.get(i).getProductid()).getProductname());
        }
        RespBean respBean=RespBean.error("新加错误！");
        if(list!=null) {
            respBean=RespBean.ok("ok",list);
            respBean.setTotleSize(ordersService.getCount());
        }
        return respBean;
    }
    @GetMapping(value="/order/getCusList")
    public RespBean getCusList(Integer beginPage, Integer pageSize){
        List<Customer> customerList =new ArrayList<>();
        List<Orders> list = ordersService.selectForPage(beginPage, pageSize);
        for(int i =0 ; i<list.size() ;i++){
            customerList.add(cusService.selectByNo(list.get(i).getCustomerid()));
        }
        RespBean respBean=RespBean.error("新加错误！");
        if(customerList!=null) {
            respBean=RespBean.ok("ok",customerList);
            respBean.setTotleSize(ordersService.getCount());
        }
        return respBean;
    }
}
